<template>
  <div>
    <div class="chart-container" v-if="userRole === 'manger'">
      <div ref="barChart" class="chart"></div>
      <div ref="pieChart" class="chart"></div>
    </div>
    <div class="chart-container" v-if="userRole === 'manger'">
      <div ref="barChartFee" class="chart"></div>
      <div ref="pieChartState" class="chart"></div>
    </div>
    <div class="chart-container" v-if="userRole === 'driver'">
      <div class="welcome-text">欢迎登录骏达出租车公司管理系统</div>
    </div>
  </div>
</template>

<script>
import * as echarts from 'echarts';
import axios from 'axios';
import { searchCarList, fetchBarChartData, fetchPieChartData, fetchBarChartDataFee, fetchPieChartDataState } from '@/api/system/car';

export default {
  data() {
    return {
      userRole: '', // 用户角色
    };
  },
  mounted() {
    this.fetchBarChartData1();
    this.fetchPieChartData1();
    this.fetchBarChartData2();
    this.fetchPieChartData2();
    this.userRole = this.getUserRoleFromCookie();
  },
  methods: {
    getUserRoleFromCookie() {
      const cookies = document.cookie.split(';');
      for (let i = 0; i < cookies.length; i++) {
        const cookie = cookies[i].trim();
        if (cookie.startsWith('User-Role=')) {
          console.log("-------------------------------99")
          console.log(cookie.substring('User-Role='.length))
          return cookie.substring('User-Role='.length);
        }
      }
      return ''; // 如果找不到User-Role cookie，则返回空字符串
    },
  
    fetchBarChartData1() {
      fetchBarChartData()
        .then(response => {
          const data = response;
          this.updateBarChart(data);
        })
        .catch(error => {
          console.error(error);
        });
    },
    fetchBarChartData2() {
      fetchBarChartDataFee()
        .then(response => {
          const data = response;
          this.updateBarChartFee(data);
        })
        .catch(error => {
          console.error(error);
        });
    },
    fetchPieChartData1() {
      fetchPieChartData()
        .then(response => {
          const data = response;
          this.updatePieChart(data);
        })
        .catch(error => {
          console.error(error);
        });
    },
    fetchPieChartData2() {
      fetchPieChartDataState()
        .then(response => {
          const data = response;
          this.updatePieChartState(data);
        })
        .catch(error => {
          console.error(error);
        });
    },
    updateBarChart(data) {
      const chart = echarts.init(this.$refs.barChart);
      console.log("--------------------------------+")
      console.log(data)
      const option = {
        title: {
          text: '驾驶员年龄图'
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          }
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: {
          data: data.xAxisData
        },
        yAxis: {},
        series: [{
          name: '人数',
          type: 'bar',
          data: data.seriesData
        }]
      };
      chart.setOption(option);
    },
    updateBarChartFee(data) {
      const chart = echarts.init(this.$refs.barChartFee);
      console.log("--------------------------------+00")
      console.log(data)
      const option = {
        title: {
          text: '运营车辆费用图'
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          }
        },
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: {
          data: data.xAxisData
        },
        yAxis: {},
        series: [{
          name: '金额',
          type: 'bar',
          data: data.seriesData
        }]
      };
      chart.setOption(option);
    },
    updatePieChart(data) {
      const chart = echarts.init(this.$refs.pieChart);
      const option = {
        title: {
          text: '车辆经营方式数量比例',
          left: 'center'
        },
        tooltip: {
          trigger: 'item'
        },
        legend: {
          orient: 'vertical',
          left: 'left'
        },
        series: [
          {
            name: '经营类型',
            type: 'pie',
            data: data
          }
        ]
      };
      chart.setOption(option);
    },
    updatePieChartState(data) {
      const chart = echarts.init(this.$refs.pieChartState);
      const option = {
        title: {
          text: '车辆状态',
          left: 'center'
        },
        tooltip: {
          trigger: 'item'
        },
        legend: {
          orient: 'vertical',
          left: 'left'
        },
        series: [
          {
            name: '经营类型',
            type: 'pie',
            data: data
          }
        ]
      };
      chart.setOption(option);
    },
  }
}
</script>

<style>
.chart-container {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
}

.chart {
  width: 45%;
  height: 300px;
}

.welcome-text {
  font-size: 28px; /* 设置为所需的字体大小 */
}
</style>